defmodule UserLib do
	@moduledoc false
	require ProjectType
	#    defstruct id: "",
	#              name: "",
	#              proj_type: "",
	#              project_id: "",
	#              proj_id: "",
	#              phone: "",
	#              node: "",
	#              ws_pid: nil,
	#              token: "",
	#              roles: [],
	#              access: []

	def deepr_default(proj_type, project_id) do
		%{__struct__: User, proj_type: proj_type, project_id: project_id}
	end

	def get_name(user) do
		user["name"]
	end

	def get_roles(user) do
		user["roles"]
	end

	def get_slab_roles(user) do
		user["slab_roles"]
	end

	def get_project_id(user) do
		user["project_id"]
	end

	def get_user_id(user) do
		user["id"]
	end

	def get_token(user) do
		user["token"]
	end

	def get_phone(user) do
		user["phone"]
	end

	def get_project_type(user) do
		user["project_type"]
	end

	def get_ip(user) do
		user["ip"]
	end

	def get_node(user) do
		user["node"]
	end

	def get_access(user) do
		user["access"]
	end

	# websocket pid
	def get_ws_pid(user) do
		user["ws_pid"]
	end

	def get_last_logout_ts(user) do
		user["last_logout_ts"]
	end

	def get_score(user) do
		user["score"]
	end

	def get_proj_name(user) do
		user["proj_name"]
	end

	def set_project_id(user, project_id) do
		Map.put(user, "project_id", project_id)
	end

	def set_user_info(user, project_id, project_type, ip) do
		Map.put(user, "project_id", project_id)
		|> Map.put("project_type", project_type)
		|> Map.put("ip", ip)
	end
	def set_user_info(user, info) do
		Map.merge(user, info)
	end

	def get_user_field(proj_type, field, default \\ "") do
		if field != nil do
			field
			|> Jason.decode!()
			|> Map.get(proj_type, default)
		else
			field
		end
	end

	def format_user_view(proj_type, user) do
		Enum.reduce(
			user,
			user,
			fn ({key, v}, acc) ->
				Map.put(acc, key, format_user(proj_type, key, v))
			end
		)
	end

	def json_key() do
		["score", "receipt_titles"]
	end

	def format_user(nil, _) do
		nil
	end
	def format_user(user, proj_type) do
		Enum.reduce(
			user,
			user,
			fn ({k, v}, acc) ->
				Map.put(acc, k, format_user(proj_type, k, v))
			end
		)
	end


	#    defp format_user(proj_type, "name" = key, v) do
	#        Jason.decode!(v)
	#        |> Map.get(proj_type, "0000000")
	#    end
	defp format_user(proj_type, "score", v) do
		Jason.decode!(v)
		|> Map.get(proj_type, 0)
	end
	#    defp format_user(proj_type, "imgs" = key, v) do
	#        Jason.decode!(v)
	#        |> Map.get(proj_type, "{}")
	#    end
	defp format_user(proj_type, "receipt_titles", v) do
		Jason.decode!(v)
		|> Map.get(proj_type, "{}")
	end
	defp format_user(proj_type, "last_login_project", v) do
		Jason.decode!(v)
		|> Map.get(proj_type, nil)
	end
	#    defp format_user(proj_type, "en_password" = key, v) do
	#        Jason.decode!(v)
	#        |> Map.get(proj_type, nil)
	#    end
	defp format_user(_, _, v) do
		v
	end

	def get_passwd_key(proj_type) do
		proj_type <> "_en_password"
	end

	def get_role_key(proj_type) do
		proj_type <> "_roles"
	end

	# 用户更新信息时(调用DoUserImpl-update接口), 不允许改变的值
	def update_cant_change() do
		[
			"id",
			"imgs",
			"score",
			"phone",
			"address",
			"slab_en_password",
			"envp_en_password",
			"register_time",
			"last_login_time",
			"receipt_titles",
			"last_login_project",
		]
	end

	def other_view_keys() do
		[
			"name",
			"phone",
			"id",
			"imgs",
		]
	end

end
